﻿using System;
using System.Text.RegularExpressions;

namespace Obsidian.Tools
{
    public class TypeParse
    {
        #region 将对象转换为String类型
        /// <summary>
        /// 将对象转换为String类型
        /// </summary>
        /// <param name="expression">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        public static string ObjToStr(object expression, string defValue)
        {
            if (expression != null)
            {
                return expression.ToString();
            }
            return defValue;
        }
        #endregion

        #region 将对象转换为Long类型
        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        public static long StrToLong(object expression, long defValue)
        {
            if (expression != null)
            {
                return StrToLong(expression.ToString(), defValue);
            }
            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        private static long StrToLong(string strValue, long defValue)
        {
            if (string.IsNullOrEmpty(strValue) || strValue.Trim().Length >= 15 || !Regex.IsMatch(strValue.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defValue;
            long rv;
            if (long.TryParse(strValue, out rv))
                return rv;
            return defValue;
        }
        #endregion

        #region 将对象转换为Int32类型
        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        public static int StrToInt(object expression, int defValue)
        {
            if (expression != null)
            {
                return StrToInt(expression.ToString(), defValue);
            }
            return defValue;
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="str">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        private static int StrToInt(string strValue, int defValue)
        {
            if (string.IsNullOrEmpty(strValue) || strValue.Trim().Length >= 11 || !Regex.IsMatch(strValue.Trim(), @"^([-]|[0-9])[0-9]*(\.\w*)?$"))
                return defValue;
            int rv;
            if (Int32.TryParse(strValue, out rv))
                return rv;
            return defValue;
        }
        #endregion

        #region 将对象转换为float型
        /// <summary>
        /// 将对象转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的float类型结果</returns>
        public static float StrToFloat(object expression, float defValue)
        {
            if ((expression == null))
            {
                return defValue;
            }

            return StrToFloat(expression.ToString(), defValue);
        }

        /// <summary>
        /// 将对象转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的float类型结果</returns>
        private static float StrToFloat(string strValue, float defValue)
        {
            if ((strValue == null) || (strValue.Length > 10))
            {
                return defValue;
            }

            float intValue = defValue;
            if (strValue != null)
            {
                bool IsFloat = Regex.IsMatch(strValue, @"^([-]|[0-9])[0-9]*(\.\w*)?$");
                if (IsFloat)
                {
                    float.TryParse(strValue, out intValue);
                }
            }
            return intValue;
        }
        #endregion

        #region 将对象转换为DateTime
        /// <summary>
        /// 将对象转换为DateTime
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的DateTime类型结果</returns>
        public static DateTime StrToDateTime(object expression, DateTime defValue)
        {
            if (expression != null && !string.IsNullOrEmpty(expression.ToString()))
            {
                return StrToDateTime(expression.ToString(), defValue);
            }
            return defValue;
        }

        /// <summary>
        /// 将对象转换为DateTime
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的DateTime类型结果</returns>
        private static DateTime StrToDateTime(string strValue, DateTime defValue)
        {
            DateTime tmp;
            //if (DateTime.TryParse(strValue, new System.Globalization.CultureInfo("zh-cn", true), System.Globalization.DateTimeStyles.None, out tmp))
            if (DateTime.TryParse(strValue, out tmp))
                return tmp;
            return defValue;
        }
        #endregion

        #region 将对象转换为decimal
        /// <summary>
        /// 将对象转换为decimal
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的decimal类型结果</returns>
        public static decimal StrToDecimal(object expression, decimal defValue)
        {
            if (expression != null && !string.IsNullOrEmpty(expression.ToString()))
            {
                return StrToDecimal(expression.ToString(), defValue);
            }
            return defValue;

        }

        /// <summary>
        /// 将对象转换为decimal
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的decimal类型结果</returns>
        private static decimal StrToDecimal(string strValue, decimal defValue)
        {
            decimal tmp;
            if (decimal.TryParse(strValue, out tmp))
                return tmp;
            return defValue;
        }
        #endregion

        #region 将对象转换为double
        /// <summary>
        /// 将对象转换为double
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的double类型结果</returns>
        public static double StrToDouble(object expression, double defValue)
        {
            if (expression != null && !string.IsNullOrEmpty(expression.ToString()))
            {
                return StrToDouble(expression.ToString(), defValue);
            }
            return defValue;
        }
        private static double StrToDouble(string strValue, double defValue)
        {
            double tmp;
            if (double.TryParse(strValue, out tmp))
                return tmp;
            return defValue;
        }
        #endregion

        #region 将对象转换为Bool
        /// <summary>
        /// 将对象转换为Bool,[1或true为true,0或false为false]
        /// </summary>
        /// <param name="expression">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的Bool类型结果</returns>
        public static bool StrToBool(object expression, bool defValue)
        {
            if (expression != null && !string.IsNullOrEmpty(expression.ToString()))
            {
                return StrToBool(expression.ToString().ToLower(), defValue);
            }
            return defValue;
        }
        private static bool StrToBool(string strValue, bool defValue)
        {
            switch (strValue)
            {
                case "0":
                case "false":
                    return false;
                case "1":
                case "true":
                    return true;
            }
            return defValue;
        }
        #endregion
    }
}
